//
// Copyright (C) 2024 Kumo group inc.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <krpc/rpc/admin/admin.h>
#include <krpc/rpc/admin/handlers.h>
#include <krpc/rpc/server.h>
#include <krpc/rpc/admin/connections.h>
#include <krpc/rpc/admin/flags.h>
#include <krpc/rpc/admin/launch.h>
#include <krpc/rpc/admin/protobufs.h>
#include <memory>

namespace krpc {
    turbo::Status admin_init(Server *server) {
        auto *instance = RestfulService::instance();
        instance->set_processor("/admin/health", krpc::admin::health_func);
        instance->set_processor("/admin/version", krpc::admin::version_func);
        // connections
        auto lc = std::make_unique<ListConnectionProcessor>();
        STATUS_RETURN_IF_ERROR(lc->initialize(server));
        std::shared_ptr<RestfulProcessor> lc_ptr(lc.release());
        instance->set_processor("/admin/connections", lc_ptr);
        LOG(INFO)<<"connection admin done";
        // socket info
        auto socket_info = std::make_unique<SocketInfoProcessor>();
        STATUS_RETURN_IF_ERROR(socket_info->initialize(server));
        std::shared_ptr<RestfulProcessor> socket_info_ptr(socket_info.release());
        instance->set_processor("/admin/sockets", socket_info_ptr);
        LOG(INFO)<<"socket info admin registry done";

        auto list_flags = std::make_unique<ListFlagsProcessor>();
        STATUS_RETURN_IF_ERROR(list_flags->initialize(server));
        std::shared_ptr<RestfulProcessor> list_flags_ptr(list_flags.release());
        instance->set_processor("/admin/flags", list_flags_ptr);
        LOG(INFO)<<"flags admin registry done";

        auto flags_rest = std::make_unique<ResetFlagsProcessor>();
        STATUS_RETURN_IF_ERROR(flags_rest->initialize(server));
        std::shared_ptr<RestfulProcessor> flags_rest_ptr(flags_rest.release());
        instance->set_processor("/admin/flags/reset", flags_rest_ptr);
        LOG(INFO)<<"flags reset admin registry done";

        auto list_launch = std::make_unique<ListLaunchProcessor>();
        STATUS_RETURN_IF_ERROR(list_launch->initialize(server));
        std::shared_ptr<RestfulProcessor> list_launch_ptr(list_launch.release());
        instance->set_processor("/admin/launch", list_launch_ptr);
        LOG(INFO)<<"launch info admin registry done";


        auto list_proto = std::make_unique<ListProtobufProcessor>();
        STATUS_RETURN_IF_ERROR(list_proto->initialize(server));
        std::shared_ptr<RestfulProcessor> list_proto_ptr(list_proto.release());
        instance->set_processor("/admin/protobufs", list_proto_ptr);
        LOG(INFO)<<"protobuf info admin registry done";

        return turbo::OkStatus();
    }
} // namespace krpc
